﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MvcDeviceManager.Models;
using MvcDeviceManager.ViewModels;
using MvcDeviceManager.Util;

namespace MvcDeviceManager.Service
{

    public class DeviceTypeService
    {
 
        public IList<DeviceTypeView> selectAllDeviceType()
        {
            return DaoUtil.getSqlMapper().QueryForList<DeviceTypeView>("selectAllDeviceType", null);
        }

        public void AddDeviceType(DeviceType deviceType)
        {
            DeviceType temp = GetDeviceTypeByDeviceTypeId(deviceType.DeviceTypeId);
            if (temp == null)
            {
                DaoUtil.getSqlMapper().Insert("insertDeviceType", deviceType);
            }
            else
            {
                throw new Exception(String.Format("对不起，设备编号[{0}]已经存在!", deviceType.DeviceTypeId));
            }
            
        }
        public DeviceType GetDeviceTypeByDeviceTypeId(string deviceTypeId)
        {
            return DaoUtil.getSqlMapper().QueryForObject<DeviceType>("selectByDeviceTypeId", deviceTypeId);
        }

        public void UpdateDeviceType(DeviceType deviceType)
        {
            DaoUtil.getSqlMapper().Update("updateDeviceType", deviceType);
        }

        public void deleteDeviceTypeByDeviceTypeId(string id)
        {
            IList<Device> list = DaoUtil.getSqlMapper().QueryForList<Device>("selectDeviceByDeviceTypeId", id);
            if (list.Count > 0)
            {
                throw new Exception(String.Format("对不起，设备分类[{0}]下，还有[{1}]部设备，所以不能删除该分类！", id, list.Count));
            }
            else
            {
                DaoUtil.getSqlMapper().Delete("deleteByDeviceTypeId", id);
            }
           
        }
    }
}